package com.ysy.logistics.controller.sale;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.myenum.StatusEnum;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.sale.Customer;
import com.ysy.logistics.entities.stock.StoreGoods;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.service.sale.CustomerService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/4/7 22:01
 **/
@Slf4j
@RestController
@RequestMapping(value = "/customer",name = "客户")
@Tag(name = "客户管理",description = "customer表的CRUD")
@CrossOrigin
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @PostMapping(value = "/listByPage")
    @Operation(summary = "分页查询客户",description = "分页查询客户")
    @OperateLog("查询客户")
    public ResultData<Page<Customer>> listByPage(@RequestBody PageInfo<Customer> pageInfo){
        return ResultData.success(customerService.listByPage(pageInfo));
    }

    @PostMapping(value = "/save")
    @Operation(summary = "保存/更新客户",description = "保存客户")
    @OperateLog("保存/更新客户")
    public ResultData save(@RequestBody Customer customer){
        log.debug("保存/更新客户:{}",customer);
        customerService.saveOrUpdate(customer);
        return ResultData.success();
    }

    @GetMapping(value = "/changeStatus/{id}")
    @Operation(summary = "修改客户状态",description = "修改客户状态")
    @OperateLog("修改客户状态")
    public ResultData changeStatus(@PathVariable("id") String id){
        log.debug("修改客户状态:{}",id);
        Customer customer = customerService.getById(id);
        if (Objects.isNull(customer)){
            return ResultData.fail("客户不存在");
        }
        if (StringUtils.equals(customer.getStatus(), StatusEnum.ENABLE.getCode())){
            customer.setStatus(StatusEnum.DISABLE.getCode());
        }else {
            customer.setStatus(StatusEnum.ENABLE.getCode());
        }
        customerService.updateById(customer);
        return ResultData.success();
    }

    @GetMapping(value = "/delete/{id}")
    @Operation(summary = "删除客户",description = "删除客户")
    @OperateLog("删除客户")
    public ResultData delete(@PathVariable("id") String id){
        log.debug("删除客户:{}",id);
        //查询出客户
        Customer customer = customerService.getById(id);
        if (Objects.isNull(customer)){
            return ResultData.fail("客户不存在");
        }
        if (StringUtils.equals(customer.getStatus(), StatusEnum.ENABLE.getCode())){
            return ResultData.fail("客户状态为启用，不能删除");
        }
        customerService.removeById(id);
        return ResultData.success();
    }

    @GetMapping(value = "/getOnlyNameList")
    public ResultData getOnlyNameList(){
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getStatus,StatusEnum.ENABLE.getCode());
        List<Customer> list = customerService.list(queryWrapper);
        return ResultData.success(list);
    }
}
